home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8182 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need HELP by a C hacker....
  5. Date: 1 Mar 1996 13:31:56 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h7qccINN8m9@anvil.ugrad.cs.ubc.ca>
  8. References: <4h5mhv$mh9@newsstand.cit.cornell.edu>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h5mhv$mh9@newsstand.cit.cornell.edu>,
  12. NOBODY  <rezab@nova.npac.syr.edu> wrote:
  13. >Hello Everyone.  
  14. >
  15. >I have a question:
  16. >
  17. >I want to declare a function
  18. >
  19. >
  20. >_____ foo(void){
  21. >       return foo;
  22. >  }
  23. >
  24. >Ususally _____ is the type of the thing that I am returning.  
  25. >In this case it's a pointer to a function that returns a pointer
  26. >to a function.  Does anyone have any ideas?  
  27. >I have gone nuts trying to figure this one out.  
  28. >BTW, I am using the gcc compiler (My microsoft compiler does not allow
  29. >anything like this to be declared).   
  30.  
  31. Your compiler is broken (assuming you have done your part correctly) if it will
  32. not take the declaration. It is valid for a function to return a pointer to a
  33. function, and it is most certain that the foo identifier is declared in the
  34. scope inside foo() itself---otherwise recursion with functions that return
  35. types other than int would not be possible without a prototype!
  36.  
  37. What the compiler should be complaining about is that what you are returning is
  38. not compatible with the declared return type.
  39.  
  40. Also, if you are trying to replace ______ with "void (*)(void)", which is a
  41. type expression for a pointer to a function returning void with no arguments,
  42. you are wrong. Such type expressions are only suitable in syntactic units like
  43. casts and sizeof(), and are not suitable in a declaration, where an explicit
  44. name is required to be infixed. It's a common beginner mistake to expect
  45. cast-form type expresions to be suitable in declarations.
  46.  
  47. Try:
  48.  
  49. void (* foo(void))(void)
  50.  
  51. {
  52.     return foo;
  53. }
  54.  
  55. It states that foo is function which takes no arguments and which returns a
  56. pointer to a function which also has no arguments which returns a pointer to
  57. void. 
  58.  
  59. There doesn't appear to be a way to define foo such that it returns its own
  60. type, since the inner declaration (which declares a return type T) would have
  61. to contain a full embedding of the outer declaration (the function having
  62. arguments X returning T). I hope you realize that this is not possible without
  63. infinite regress.
  64.  
  65. What is the point of returning a pointer to a function from the function
  66. itself? You can just write "foo" anywhere you need its value. If you can call
  67. foo, then you already know its address, so there is no point in computing it
  68. inside foo.
  69. -- 
  70.  
  71.